package com.zb.service.imp.middleware;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zb.dao.ext.charger.OmsChargerDao;
import com.zb.entity.base.JsonCommon;
import com.zb.entity.charger.OmsCharger;
import com.zb.service.imp.middleware.vo.ChargerMsgHeader;
import com.zb.service.imp.middleware.vo.ChargerReplyMsgHeader;
import com.zb.tcp.rpc.as.SystemAppServerRpcClient;
import com.zb.tcp.rpc.as.client.constant.AppServerConstant;
import com.zb.tcp.rpc.as.client.entity.AppExceptionNotifyParam;
import com.zb.tcp.rpc.as.client.entity.AppReservationNotifyParam;
import com.zb.util.OmsConstant;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Component
public class NonNormalResponseHandlerImpl implements NonNormalResponseHandler
{
	private Log log = LogFactory.getLog(NonNormalResponseHandlerImpl.class);

	@Autowired
	OmsChargerDao omsChargerDao;

//	@Autowired
//	SystemAppServerRpcClient systemAppServerRpcClient;

	@Override
	public void process(ChargerReplyMsgHeader responseHeader)
	{
		if (responseHeader.getResponseCode().equals(OmsConstant.MsgResponseCodeEnum.Normal.value()))
			return;
		/*
		如果是几大主要业务, 预约, 取消预约, 充电, 取消充电
		当收到异常响应码时, 将发送异常推送给appserver
		 */

		String action = null;
		if ("51".equals(responseHeader.getResponseType())) //预约  电桩回复（0x51）
		{
			action = AppServerConstant.EXCEPTION_NOTIFY_ACTION_RESERVATION;
		}
		else if ("52".equals(responseHeader.getResponseType())) //预约取消  电桩回复（0x52）
		{
			action = AppServerConstant.EXCEPTION_NOTIFY_ACTION_CANCEL_RESERVATION;
		}
		else if ("53".equals(responseHeader.getResponseType())) //充电  电桩回复（0x53）
		{
			action = AppServerConstant.EXCEPTION_NOTIFY_ACTION_CHARGE;
		}
		else if ("54".equals(responseHeader.getResponseType())) //关闭充电 电桩回复（0x54）
		{
			action = AppServerConstant.EXCEPTION_NOTIFY_ACTION_END_CHARGE;
		}

		if (action == null)
			return;

		StringBuffer sql = new StringBuffer();

		sql.append(" SELECT s.station_name, bill.* FROM ( ");
		sql.append(" SELECT '").append(AppServerConstant.EXCEPTION_NOTIFY_ACTION_RESERVATION).append("' biztype, bs.charger_id, bs.port_no, bs.user_pay_card, date_format(start_time,'%Y-%m-%d %H:%i:%S') start_time, bs.bespeak_jrnl jrnl ");
		sql.append("   FROM oms_charger_bespeak_bill_record bs WHERE bs.oms_msg_bespeak_jrnl = ? AND bs.valid_flag = 1 ");
		sql.append(" UNION ALL ");
		sql.append(" SELECT '").append(AppServerConstant.EXCEPTION_NOTIFY_ACTION_CANCEL_RESERVATION).append("' biztype, bc.charger_id, bc.port_no, bc.user_pay_card, date_format(bc.start_time,'%Y-%m-%d %H:%i:%S') start_time, bc.bespeak_jrnl jrnl ");
		sql.append("   FROM oms_charger_bespeak_bill_record bc WHERE bc.oms_msg_bespeak_end_jrnl = ? AND bc.valid_flag = 1 ");
		sql.append(" UNION ALL ");
		sql.append(" SELECT '").append(AppServerConstant.EXCEPTION_NOTIFY_ACTION_CHARGE).append("' biztype, cs.charger_id, cs.port_no, cs.user_pay_card, date_format(cs.start_time,'%Y-%m-%d %H:%i:%S') start_time, cs.charge_jrnl jrnl ");
		sql.append("   FROM oms_charger_charge_bill_record cs WHERE cs.msg_charge_jrnl = ? AND cs.valid_flag = 1 ");
		sql.append(" UNION ALL ");
		sql.append(" SELECT '").append(AppServerConstant.EXCEPTION_NOTIFY_ACTION_END_CHARGE).append("' biztype, ce.charger_id, ce.port_no, ce.user_pay_card, date_format(ce.start_time,'%Y-%m-%d %H:%i:%S') start_time, ce.charge_jrnl jrnl  ");
		sql.append("   FROM oms_charger_charge_bill_record ce WHERE ce.msg_charge_end_jrnl = ? AND ce.valid_flag = 1 ");
		sql.append(" ) bill ");
		sql.append(" INNER JOIN oms_charger c ON c.charger_id = bill.charger_id ");
		sql.append(" INNER JOIN oms_station s ON s.station_id = c.station_id ");

		log.info(sql.toString());
		String serverSerialNo = responseHeader.getServerSerialNo();
		List<Map<String, Object>> rs = omsChargerDao.findDataBySql(sql.toString(), serverSerialNo, serverSerialNo, serverSerialNo, serverSerialNo);


		if (rs != null && !rs.isEmpty())
		{
			Map<String, Object> record = rs.get(0);
			if (!action.equals(record.get("biztype")))
				return;

			AppExceptionNotifyParam param = new AppExceptionNotifyParam();
			param.setAction(action);
			param.setChargerId((String) record.get("charger_id"));
			param.setStationName((String) record.get("station_name"));
			param.setUserCard((String) record.get("user_pay_card"));

			if (param.getAction().equals(AppServerConstant.EXCEPTION_NOTIFY_ACTION_RESERVATION) || param.getAction().equals(AppServerConstant.EXCEPTION_NOTIFY_ACTION_CANCEL_RESERVATION))
				param.setReservationSn((String) record.get("jrnl"));
			else if (param.getAction().equals(AppServerConstant.EXCEPTION_NOTIFY_ACTION_CHARGE) || param.getAction().equals(AppServerConstant.EXCEPTION_NOTIFY_ACTION_END_CHARGE))
				param.setChargeSn((String) record.get("jrnl"));

			param.setStartTime((String) record.get("start_time"));
			param.setResponseCode(responseHeader.getResponseCode());
//			JsonCommon<String> result = systemAppServerRpcClient.exceptionNotify(param);
			JsonCommon<String> result = null;
			log.info(result);
			if (result != null)
			{
				log.info(result.getCode());
				log.info(result.getMsg());
			}
		}
	}
}
